<template>
  <div class="hello">
    <h1>{{ myInfo }}</h1>
    <h1>{{ sheInfo }}</h1>
    <h1>{{ love }}</h1>
    <h1>{{ say }}</h1>
    <button @click="aaa">改变</button>
    <button @click="putMyInfo2">action</button>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import * as types from '@/store/mutation-types'
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  methods: {
    // mapActions 类似user里定义的action集合，取出了两个方法，然后在页面就可以直接使用了
    ...mapActions([
      'putMyInfo',
      'putMyInfo2'
    ]),
    aaa () {
      const myInfo = {
        name: '果果',
        age: 22
      }
      const sheInfo = {
        name: 'tnq',
        age: 21
      }
      const loveArr = ['苹果，栗子，草莓，菠萝']
      this.$store.commit(types.SET_MY_INFO, myInfo) // 直接调用mutations改变值（不可异步）
      this.$store.commit(types.SET_SHE_INFO, sheInfo)
      this.$store.commit(types.SET_LOVE, loveArr)
      this.$store.dispatch('putMyInfo').then(req => {
        // 调用user里面的action中定义的方法putMyInfo（异步）
        console.dir(req)
      })
    }
  },
  computed: {
    ...mapGetters([
      'myInfo',
      'sheInfo',
      'love',
      'say'
    ])
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
